package com.etv.launcher.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 */
public class TimeUtil {
    public static final SimpleDateFormat M_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat FORMAT_DAY = new SimpleDateFormat("yyyy-MM-dd");

    public static final SimpleDateFormat FORMAT_DAY_HOUR = new SimpleDateFormat("yyyy-MM-dd HH");

    public static String formatDateToMStr(Date date){
        return M_FORMAT.format(date);
    }

    public static String formatDateToStr(Date date){
        return FORMAT.format(date);
    }
    public static String formatDateToDayStr(Date date){
        return FORMAT_DAY.format(date);
    }

    public static String formatDateToHourStr(Date date){
        return FORMAT_DAY_HOUR.format(date);
    }

    public static String formatDataToStr(Date date,String format){
        return new SimpleDateFormat(format).format(date);
    }

    public static String getCurrentTimeStr(){
        return FORMAT.format(new Date());
    }

    public static String getCurrentTimeMStr(){
        return M_FORMAT.format(new Date());
    }

    public static Date formatMStrToDate(String dateStr){
        Date date = null;
        try {
            date = M_FORMAT.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    public static Date formatStrToDate(String dateStr){
        Date date = null;
        try {
            date = FORMAT.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    public static Date addDateAmount(Date currentDate,int field,int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(field,amount);
        return calendar.getTime();
    }

    public static Date getFirstDay(Date currentTime,int calenderField){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.set(calenderField,1);
        return calendar.getTime();
    }


    public static Date getEndDay(Date currentTime,int calenderField){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.set(calenderField,calendar.getActualMaximum(calenderField));
        return calendar.getTime();
    }

    public static Date getWholePointHour(Date date,int hour){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,hour);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);

        return calendar.getTime();
    }
}
